package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.zhgd.util.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.dto.DeviceTypeDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NLaboratoryDeviceBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryDeviceVo;
import com.ruoyi.zhgd.domain.NLaboratoryDevice;
import com.ruoyi.zhgd.mapper.NLaboratoryDeviceMapper;
import com.ruoyi.zhgd.service.INLaboratoryDeviceService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 实验室设备信息Service业务层处理
 *
 * @author zyb
 * @date 2023-09-12
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NLaboratoryDeviceServiceImpl implements INLaboratoryDeviceService {

    private final NLaboratoryDeviceMapper baseMapper;

    /**
     * 查询实验室设备信息
     */
    @Override
    public NLaboratoryDeviceVo queryById(String id){
        return baseMapper.selectVoByIdz(id);
    }

    /**
     * 查询实验室设备信息列表
     */
    @Override
    public TableDataInfo<NLaboratoryDeviceVo> queryPageList(NLaboratoryDeviceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NLaboratoryDevice> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryDeviceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询实验室设备信息列表
     */
    @Override
    public List<NLaboratoryDeviceVo> queryList(NLaboratoryDeviceBo bo) {
        LambdaQueryWrapper<NLaboratoryDevice> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NLaboratoryDevice> buildQueryWrapper(NLaboratoryDeviceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NLaboratoryDevice> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBaseCode()), NLaboratoryDevice::getBaseCode, bo.getBaseCode());
        lqw.like(StringUtils.isNotBlank(bo.getLaboratoryName()), NLaboratoryDevice::getLaboratoryName, bo.getLaboratoryName());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), NLaboratoryDevice::getType, bo.getType());
        lqw.like(StringUtils.isNotBlank(bo.getName()), NLaboratoryDevice::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), NLaboratoryDevice::getModel, bo.getModel());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), NLaboratoryDevice::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getManufacturer()), NLaboratoryDevice::getManufacturer, bo.getManufacturer());
        lqw.eq(StringUtils.isNotBlank(bo.getServicePhone()), NLaboratoryDevice::getServicePhone, bo.getServicePhone());
        lqw.eq(bo.getNum() != null, NLaboratoryDevice::getNum, bo.getNum());
        lqw.eq(bo.getCreator() != null, NLaboratoryDevice::getCreator, bo.getCreator());
        lqw.eq(bo.getCreateDate() != null, NLaboratoryDevice::getCreateDate, bo.getCreateDate());
        lqw.eq(bo.getUpdater() != null, NLaboratoryDevice::getUpdater, bo.getUpdater());
        lqw.eq(bo.getUpdateDate() != null, NLaboratoryDevice::getUpdateDate, bo.getUpdateDate());
        return lqw;
    }

    /**
     * 新增实验室设备信息
     */
    @Override
    public Boolean insertByBo(NLaboratoryDevice bo) {
        NLaboratoryDevice add = BeanUtil.toBean(bo, NLaboratoryDevice.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改实验室设备信息
     */
    @Override
    public Boolean updateByBo(NLaboratoryDevice bo) {
        NLaboratoryDevice update = BeanUtil.toBean(bo, NLaboratoryDevice.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NLaboratoryDevice entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除实验室设备信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<Map> queryPage(String name) {
        List<Map> list = baseMapper.queryPages(name);
        return list;
    }

    @Override
    public PageUtils deviceList(Page page, Map<String, Object> params) {
        if(params.get("id") == null){
            params.put("id", LoginHelper.getDeptId().toString());
        }
        List<NLaboratoryDeviceVo> maps = baseMapper.deviceList(page, params);
        return new PageUtils(page.setRecords(maps));
    }

    @Override
    public List<DeviceTypeDto> typeList() {
        return baseMapper.typeList();
    }
}
